import EsConfig from "./EsConfig.js"

const UrlCfg = {
	bizLoginUrlOfWeixin: "http://8.129.12.13:8087/index.php/api/user/wxlogin",
	code2SessionUrl: "http://8.129.12.13:8087/index.php/api/user/wxmpcode2session",
}

class EsWxLogin {
	static debug(message) {
		uni.showModal({
			content: message
		})
	}
	/**
	 * 微信登录（适用于app）
	 */
	static wxlogin(success, fail) {
		uni.login({
			provider: "weixin",
			success: (resLogin) => {
				// EsWxLogin.debug("微信登录成功 uni.login() " + (resLogin ? JSON.stringify(resLogin) : '...'));
				uni.getUserInfo({
					provider: "weixin",
					success: (resUserInfo) => {
						// EsWxLogin.debug("微信获取用户信息成功 uni.getUserInfo() " + (resUserInfo ? JSON.stringify(resUserInfo) : '...'));
						let wxUser = resUserInfo.userInfo;
						// 登录开发服务器
						EsWxLogin.loginBizServer(wxUser, success, fail);
					},
					fail: (resUserInfo) => {
						EsWxLogin.debug("微信获取用户信息失败 uni.getUserInfo() " + (resUserInfo ? JSON.stringify(resUserInfo) : '...'));
					}
				})
			},
			fail: (resLogin) => {
				EsWxLogin.debug("微信登录失败 uni.login() " + (resLogin ? JSON.stringify(resLogin) : '...'));
			}
		})
	}
	/**
	 * 微信小程序登录（适用于小程序）
	 */
	static wxmplogin(success, fail) {
		uni.getUserProfile({
			desc: "用户登录或注册",
			success: (resProfile) => {
				// resProfile.userInfo
				uni.login({
					provider: "weixin",
					success: (resLogin) => {
						// resLogin.code
						// 通过 code2Session 接口获取 openid
						uni.request({
							url: UrlCfg.code2SessionUrl,
							data: {
								code: resLogin.code
							},
							success: (retCode2Session) => {
								let wxUser = resProfile.userInfo;
								let msgInfo = retCode2Session.data;
								if (msgInfo.status == 1) {
									// data: "{"session_key":"n2OlF2ItpdaK2NwnYk9f8w==","openid":"oiCR05ayUqlKWspsOOo_SODTjo0g"}"
									let code2sessionInfo = JSON.parse(msgInfo
										.data);
									wxUser.openId = code2sessionInfo.openid;
								}
								// 登录开发服务器
								EsWxLogin.loginBizServer(wxUser, success, fail);
							}
						})
					}
				})
			},
			fail() {

			}
		})
	}
	static testlogin(success, fail) {
		let wxUser = EsWxLogin.getBogusWxUser();
		EsWxLogin.loginBizServer(wxUser, success, fail);
	}
	static loginBizServer(wxUser, success, fail) {
		wxUser.oaid = uni.getStorageSync('OAID') || '';
		wxUser.android_id = uni.getStorageSync('ANDROID_ID') || '';
		wxUser.channel = EsConfig.channelName; // 渠道标识
		wxUser.appmarket = EsConfig.appMarket; // 渠道市场
		// 登录开发服务器
		uni.request({
			url: UrlCfg.bizLoginUrlOfWeixin,
			data: wxUser,
			method: "POST",
			success: (retBizLogin) => {
				console.log('登录开发服务器', wxUser, retBizLogin);
				let msgInfo2 = retBizLogin.data;
				if (msgInfo2.status == 0) {
					uni.showModal({
						title: "业务登录错误",
						content: msgInfo2.message
					})
					if (typeof(fail) == 'function') {
						fail(mstInfo2);
					}
					return;
				}
				// 把用户信息保存到本地缓存
				uni.setStorageSync(EsConfig.loginSessName, msgInfo2.data);
				if (typeof(success) == 'function') {
					success(msgInfo2);
				}
			}
		})
	}
	// 模拟微信登录
	static getBogusWxUser(id = 0) {
		let wxUser = {
			"openId": "oVVOL0_AFjw2eWMJ8ODfgSUgdEGg",
			"nickName": "天澜",
			"gender": 1,
			"city": "厦门",
			"province": "福建",
			"country": "中国",
			"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJiaOCnl2jzfGhL9qWL7ypLDEVpkd4ArvHdCFeryicvmXqemFK8nae6hyd1OZNRFMOWhZ03zfnc7NfA/132",
			"unionId": "oqNz_vyYVLrqAYO5QOGQbTym6M3k"
		};
		if (id == 1) {
			wxUser = {
				"openId": "ozBRl60F4hbRMy5BhYCL3WoJIbNk",
				"nickName": "兔子哥不起",
				"gender": 1,
				"city": "厦门",
				"province": "福建",
				"country": "中国",
				"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLFTvwDEkhBxLnoGmIacLggKF5Mfc9icd62fahqzLlRRBniaKXibOfDicibGiaiaKeAib9ibM7X3r3jn26Elfg/132",
				"unionId": "oqNz_v0SUVmed8DIafhkjM5Jmt6Y"
			}
		}
		return wxUser;
	}
}

export default EsWxLogin;
